from PyQt6.QtWidgets import (QMainWindow, QWidget, QHBoxLayout, 
                           QVBoxLayout, QPushButton, QStackedWidget, QMessageBox, QLabel,
                           QStyle)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QPixmap, QIcon
from .pages.model_config import ModelConfigPage
from .pages.knowledge_training import KnowledgeTrainingPage
from .pages.chat_interface import ChatInterfacePage
import os

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("千泽 v1.0.1")
        self.setMinimumSize(1200, 800)
        self.init_ui()
        
    def init_ui(self):
        # 创建主布局
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QHBoxLayout(main_widget)
        
        # 左侧菜单
        menu_layout = QVBoxLayout()
        
        # 添加 Logo
        logo_label = QLabel()
        logo_label.setObjectName("logo_label")
        logo_path = os.path.join("src", "resources", "images", "logo.png")
        if os.path.exists(logo_path):
            pixmap = QPixmap(logo_path)
            logo_label.setPixmap(pixmap)
            logo_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        menu_layout.addWidget(logo_label)
        
        # 添加软件名称
        title_label = QLabel("千泽 v1.0.1")
        title_label.setObjectName("app_title")
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        menu_layout.addWidget(title_label)
        menu_layout.addSpacing(20)  # Logo和按钮之间的间距
        
        # 创建带图标的按钮
        icons_path = os.path.join("src", "resources", "icons")
        
        self.model_btn = QPushButton("  模型配置")
        self.model_btn.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_FileDialogDetailedView))
        self.model_btn.setIconSize(QSize(16, 16))
        
        self.training_btn = QPushButton("  知识训练")
        self.training_btn.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_DialogOpenButton))
        self.training_btn.setIconSize(QSize(16, 16))
        
        self.chat_btn = QPushButton("  模型对话")
        self.chat_btn.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_ComputerIcon))
        self.chat_btn.setIconSize(QSize(16, 16))
        
        menu_layout.addWidget(self.model_btn)
        menu_layout.addWidget(self.training_btn)
        menu_layout.addWidget(self.chat_btn)
        menu_layout.addStretch()
        
        menu_widget = QWidget()
        menu_widget.setObjectName("menu_container")
        menu_widget.setLayout(menu_layout)
        menu_widget.setFixedWidth(200)
        
        # 右侧内容区
        self.stack = QStackedWidget()
        self.model_page = ModelConfigPage()
        self.training_page = KnowledgeTrainingPage()
        self.chat_page = ChatInterfacePage()
        
        self.stack.addWidget(self.model_page)
        self.stack.addWidget(self.training_page)
        self.stack.addWidget(self.chat_page)
        
        layout.addWidget(menu_widget)
        layout.addWidget(self.stack)
        
        # 连接信号
        self.model_btn.clicked.connect(lambda: self.stack.setCurrentIndex(0))
        self.training_btn.clicked.connect(lambda: self.stack.setCurrentIndex(1))
        self.chat_btn.clicked.connect(lambda: self.stack.setCurrentIndex(2))
        
        # 设置按钮对象名
        self.model_btn.setObjectName("menu_button")
        self.training_btn.setObjectName("menu_button")
        self.chat_btn.setObjectName("menu_button")
        
        # 加载样式表
        style_path = os.path.join("src", "resources", "styles.qss")
        if os.path.exists(style_path):
            try:
                with open(style_path, 'r', encoding='utf-8') as f:
                    self.setStyleSheet(f.read())
            except Exception as e:
                print(f"加载样式表失败: {str(e)}")
        
        # 检查模型列表
        if self.model_page.refresh_models():  # 不显示消息
            # 如果成功获取模型列表，显示聊天界面
            self.stack.setCurrentWidget(self.chat_page)
            self.chat_btn.setChecked(True)
        else:
            # 如果获取模型列表失败，显示模型配置界面
            self.stack.setCurrentWidget(self.model_page)
            self.model_btn.setChecked(True)
            QMessageBox.warning(self, "提示", "请先配置模型") 